/**
 * @author AXRFO
 *
 */
public class PercolationStats {
   
	private int N;			
	private int T;			
	private double[] resultado;	
	
	/**
	 * @param  
	 * @param 
	 */
	public PercolationStats(int N, int T)
	{
		if(N<=0 || T<=0) 
			throw new IllegalArgumentException("Ilegal data.");
		
		this.N = N;
		this.T = T;
		resultado = new double[T];
		
		for(int i=0;i< T;i++)
		{
			Percolation grid = new Percolation(N);
			int result = 0;
			while(!grid.percolates())
			{
				openRandomBlockedNode(grid);
				result++;
			}
			resultado[i] = (double) result / (double) (N*N);
		}
	}
   
	
	/**
	 * @return
	 */
	public double mean()
	{
		return StdStats.mean(resultado);
	}
	
	/**
	 * 
	 * @return 
	 */
	public double stddev()
	{
		return StdStats.stddev(resultado);
	}
   
	/**
	 * 
	 * @return 
	 */
	public double confidenceLo()
	{
		return mean()-((1.96*stddev())/Math.sqrt(T));
	}
  
	/**
	 * @return 
	 */
	public double confidenceHi()
	{
		return mean()+((1.96*stddev())/Math.sqrt(T));
	}
	

	/**
	 * @param grid
	 */
	private void openRandomBlockedNode(Percolation grid)
	{
		boolean isOpen = true;
		int randomRow = 0;
		int randomCol = 0;
		while(isOpen)
		{
			randomRow = StdRandom.uniform(1,N+1);
			randomCol = StdRandom.uniform(1,N+1);
			
			isOpen = grid.isOpen(randomRow,randomCol);
		}
		grid.open(randomRow,randomCol);
	}
	
	/**
	 * @param args 
	 */
	public static void main(String[] args)
	{
		int N = new Integer(args[0]);
		int T = new Integer(args[1]);
		
		PercolationStats stats = new PercolationStats(N,T);
		
		System.out.println("mean:\t\t\t\t = " + stats.mean());
		System.out.println("stddev:\t\t\t\t = " + stats.stddev());
		System.out.println("95% confidence interval:\t = " + stats.confidenceLo() + ", " + stats.confidenceHi());
	}

}